package com.example.weatherapp.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class DateUtil {
    public static String formatHourlyTime(String isoTime) {
        try {
            SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm+08:00", Locale.CHINA);
            SimpleDateFormat outputFormat = new SimpleDateFormat("HH:mm", Locale.CHINA);
            Date date = inputFormat.parse(isoTime);
            return outputFormat.format(date);
        } catch (ParseException e) {
            return isoTime.substring(11, 16); // 失败时返回原始时间
        }
    }

    public static String formatWarningTime(String time) {
        try {
            SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm+08:00", Locale.getDefault());
            SimpleDateFormat outputFormat = new SimpleDateFormat("MM-dd HH:mm", Locale.getDefault());
            Date date = inputFormat.parse(time);
            return outputFormat.format(date);
        } catch (Exception e) {
            e.printStackTrace();
            return time;
        }
    }

    public static String formatDate(String time) {
        try {
            SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm+08:00", Locale.getDefault());
            SimpleDateFormat outputFormat = new SimpleDateFormat("MM月dd日", Locale.getDefault());
            Date date = inputFormat.parse(time);
            return outputFormat.format(date);
        } catch (Exception e) {
            e.printStackTrace();
            return time;
        }
    }

    public static String formatTideTime(String time) {
        try {
            SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm+08:00", Locale.getDefault());
            SimpleDateFormat outputFormat = new SimpleDateFormat("HH:mm", Locale.getDefault());
            Date date = inputFormat.parse(time);
            return outputFormat.format(date);
        } catch (Exception e) {
            e.printStackTrace();
            return time;
        }
    }
}